package engine
{
	import flash.geom.Point;
	import flash.utils.ByteArray;
	
	import util.MathTool;

	/**
	 * 底图信息，包括
	 * 隐形墙处理，寻路用
	 * 底图上物品列表。 
	 * @author Ryan
	 * 
	 */	
	public class Wall
	{
		public var mapId : int;
		public var mapWidth : int;
		public var mapHeight : int;
		
		private var _stepX : int;
		private var _stepY : int;
		
		private var _itemList:Array;
		
		private var _byte : ByteArray;
		
		private var _point:Point = new Point;
		
		public function Wall()
		{
		}
		
		public function get itemList():Array
		{
			return _itemList;
		}

		public function setData(byte:ByteArray) : void
		{
			//解压
			byte.uncompress();
			mapId = byte.readInt();
			mapWidth = byte.readInt();
			mapHeight = byte.readInt();
			_stepX = byte.readInt();
			_stepY = byte.readInt();
			_itemList = byte.readObject();
			_byte = new ByteArray();
			byte.readBytes(_byte,0,byte.bytesAvailable);
			// 不要忘记解压缩！
			_byte.uncompress();
		}
		
		public function clear() : void
		{
			mapId = -1;
			_byte = null;
		}
		
		public function canWalk( pixX:uint , pixY:uint ):Boolean
		{
			return !isWall(pixX,pixY);
		} 
		
		public function isWall( pixX:uint , pixY:uint ):Boolean
		{
			if( pixX < 0 || pixX > mapWidth || pixY < 0 || pixY > mapHeight ){
				return true;
			}
			if( !_byte ) {
				return true;
			}
			var checkX : int = pixX/_stepX;
			var checkY : int = pixY/_stepY;
			var index_total:int = checkY * int(mapWidth/_stepX) + checkX;
			var index_array:int = index_total / 8;
			var index_bit:int = index_total % 8;
			var mask:uint = 1 << index_bit;
			var ori:uint = _byte[index_array] ;
			var result:uint = ori & mask;		
			return Boolean(result);
		}

		public function getNearestWalkPoint( pixX:Number , pixY:Number ):Point
		{
			if( pixX < 0 || pixX > mapWidth || pixY < 0 || pixY > mapHeight ){
				return null;
			}
			if( !_byte ) {
				return null;
			}
			/*if(canWalk(pixX,pixY)) {
				_point.x = pixX;
				_point.y = pixY;
				return _point;
			}*/
			// 暂时采用先找水平方向，再找垂直方向的方法
			var op:Boolean = true;
			var startX:Number = pixX;
			var startY:Number = pixY;
			var count:int = 1;
			var min:Number = 0;
			var distance:Number = 0;
			for(var x:Number = startX;count < mapWidth / _stepX;count++)
			{
				if(count % 2 == 1){
					x += _stepX * count;
				}else{
					x -= _stepX * count;
				}
				if(canWalk(x,startY))
				{
					if(x >= 0 && x <= mapWidth)
					{
						distance = MathTool.distance(startX,startY,x,startY);
						if(distance < min || min == 0)
						{
							_point.x = x;
							_point.y = startY;
							min = distance;
						}
					}
				}
			}
			count = 1;
			for(var y:Number = startY;count < mapHeight / _stepY;count++)
			{
				if(count % 2 == 1){
					y += _stepY * count;
				}else{
					y -= _stepY * count;
				}
				if(canWalk(startX,y))
				{
					if(y >= 0 && y <= mapHeight)
					{
						distance = MathTool.distance(startX,startY,startX,y);
						if(distance < min || min == 0)
						{
							_point.x = startX;
							_point.y = y;
							min = distance;
						}
					}
				}
			}
			
			if(min > 0) return _point;
			
			return null;
		}
	}
}